const util = require("../../utils/util.js");
var tDate = require("../../utils/date.js");

const app = getApp()
const date = new Date()

Page({
  data: {
    order_id: '',
    task_type: "",
    // 任务时间
    taskTime: "",
    // 住宅位置
    taskAddress: "",
    // 任务优先级
    taskPriority: "",
    // 安装时间
    installTime: "0",
    // 备注信息
    memoInfo: "",

    selecteDate: "",
    selectTime: "",
    dateValue: [0, 0],
    dateValues: [0, 0],
    selectPicker: false,
    // 任务优先级数组
    selectTaskPriorityPicker: "0",
    score: "1",
    taskPriority_index: 0,
    taskPriorityArray: ['普通', '紧急'],

    // 今天的时间
    date: ""
  },
  // 选取任务优先级
  bindPickerTap: function () { },
  bindTaskPriorityPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var selectText = this.data.taskPriorityArray[e.detail.value];
    if (selectText === "普通") {
      this.setData({
        score: "1",
        taskPriority_index: e.detail.value
      })
    } else if (selectText === "紧急") {
      this.setData({
        score: "2",
        taskPriority_index: e.detail.value
      })
    }
  },
  // 选取处理时间
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value,
      installTime: e.detail.value
    })
  },
  onLoad(options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.setData({
      order_id: options.id,
      taskTime: options.time,
      taskAddress: options.address,
      task_type: options.task_type
    })

    console.log("都是对的多所：taskID:" + this.data.order_id);
    this.$wuxToast = app.wux(this).$wuxToast;

    //今天的时间
    var todayDate = new Date();

    todayDate.setTime(todayDate.getTime());
    var today_month = todayDate.getMonth() + 1;
    if (today_month < 10) {
      today_month = "0" + today_month;
    }
    var today_date = todayDate.getDate();
    if (today_date < 10) {
      today_date = "0" + today_date;
    }
    var todayDateStr = todayDate.getFullYear() + "-" + today_month + "-" + today_date;

    this.setData({
      date: todayDateStr,
      installTime: todayDateStr,
    })
  },
  // 备注信息
  inputMemoInfo: function (e) {
    this.setData({
      memoInfo: e.detail.value
    })
  },
  // 提交表单按钮
  submitInfo: function () {
    var installStr = this.data.installTime;
    var notesStr = this.data.memoInfo;
    var scoreStr = this.data.score;
    var taskIdStr = this.data.order_id;

    console.log("时间：" + installStr);
    console.log("备注：" + notesStr);
    console.log("11111+adminUserViewId:" + app.globalData.adminUserViewId);
    console.log("111111+token:" + app.globalData.token);
    console.log("taskID:" + taskIdStr);
    //刷新数据
    //加载提示框
    util.showLoading();
    var that = this;
    var pages = getCurrentPages()
    // var prevPage = pages[pages.length-1]  //当前界面
    var prevPage = pages[pages.length - 3]  //上一个页面

    var urlString = app.globalData.BaseURL + '/api/task/fail';
    var dataSource = util.json2Form({
      taskId: taskIdStr,
      score: scoreStr,
      reserTime: installStr,
      remark: notesStr
    });

    wx.request({
      method: "POST",
      url: urlString,
      data: dataSource,
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'adminUserViewId': app.globalData.adminUserViewId,
        'token': app.globalData.token
      },
      success: function (res) {
        util.hideToast();
        console.log("data=====" + res.data);
        var code = res.data.code;
        console.log("code=====" + code);
        // code = 300;
        if (code === 200) {
          that.$wuxToast.show({
            type: 'text',
            timer: 1000,
            color: '#fff',
            text: '上传成功',
            success: () => console.log('提交成功')
          });
          // 更改前一页面列表页的refreshLish属性，让其出现时刷新页面
          prevPage.setData({
            refreshLish: "true"
          });

          setTimeout(function () {
            wx.navigateBack({
              delta: 3
            })
          }, 1000);
        } else {
          that.$wuxToast.show({
            type: 'text',
            timer: 1000,
            color: '#fff',
            text: res.data.msg,
            success: () => console.log('上传失败，请重新提交。')
          });
        }
      },
      fail: function () {
        that.$wuxToast.show({
          type: 'text',
          timer: 1000,
          color: '#fff',
          text: '网络错误，请稍后重试。',
          success: () => console.log('网络错误，请稍后重试。')
        })
      }, complete: function () {
        util.hideToast();
      }
    })
  }

})


